package cook

import org.springframework.dao.DataIntegrityViolationException

/**
 * DishComponentController
 * A controller class handles incoming web requests and performs actions such as redirects, rendering views and so on.
 */
class DishComponentController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [dishComponentInstanceList: DishComponent.list(params), dishComponentInstanceTotal: DishComponent.count()]
    }

    def create() {
        [dishComponentInstance: new DishComponent(params)]
    }

    def save() {
		for (String key in params.keySet()) {
			if (key.contains("quantity")) {
				System.out.println(params[key])
				def bd = new BigDecimal(params[key])
				params[key] = bd
			}
		}
		
        def dishComponentInstance = new DishComponent(params)
        if (!dishComponentInstance.save(flush: true)) {
            render(view: "create", model: [dishComponentInstance: dishComponentInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'dishComponent.label', default: 'DishComponent'), dishComponentInstance.id])
        redirect(action: "show", id: dishComponentInstance.id)
    }

    def show() {
        def dishComponentInstance = DishComponent.get(params.id)
        if (!dishComponentInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'dishComponent.label', default: 'DishComponent'), params.id])
            redirect(action: "list")
            return
        }

        [dishComponentInstance: dishComponentInstance]
    }

    def edit() {
        def dishComponentInstance = DishComponent.get(params.id)
        if (!dishComponentInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'dishComponent.label', default: 'DishComponent'), params.id])
            redirect(action: "list")
            return
        }

        [dishComponentInstance: dishComponentInstance]
    }

    def update() {
		
		for (String key in params.keySet()) {
			if (key.contains("quantity")) {
				def bd = new BigDecimal(params[key])
				params[key] = bd
			}
		}
		
		
        def dishComponentInstance = DishComponent.get(params.id)
        if (!dishComponentInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'dishComponent.label', default: 'DishComponent'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (dishComponentInstance.version > version) {
                dishComponentInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'dishComponent.label', default: 'DishComponent')] as Object[],
                          "Another user has updated this DishComponent while you were editing")
                render(view: "edit", model: [dishComponentInstance: dishComponentInstance])
                return
            }
        }
		
        dishComponentInstance.properties = params		

        if (!dishComponentInstance.save(flush: true)) {
            render(view: "edit", model: [dishComponentInstance: dishComponentInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'dishComponent.label', default: 'DishComponent'), dishComponentInstance.id])
        redirect(action: "show", id: dishComponentInstance.id)
    }

    def delete() {
        def dishComponentInstance = DishComponent.get(params.id)
        if (!dishComponentInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'dishComponent.label', default: 'DishComponent'), params.id])
            redirect(action: "list")
            return
        }

        try {
            dishComponentInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'dishComponent.label', default: 'DishComponent'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'dishComponent.label', default: 'DishComponent'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
